package com.wq.httpclientcore.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wq.httpclientcore.config.DynamicDataSourceConfig;
import com.wq.httpclientcore.entities.po.DbConnectionConfig;
import com.wq.httpclientcore.entities.po.DbQueryConfig;
import com.wq.httpclientcore.entities.req.query.DbConnectionConfigREQ;
import com.wq.httpclientcore.entities.req.query.DbQueryConfigREQ;
import com.wq.httpclientcore.event.AuthConfigDeletedEvent;
import com.wq.httpclientcore.event.DbConnectionConfigDeletedEvent;
import com.wq.httpclientcore.mapper.DbConnectionConfigMapper;
import com.wq.httpclientcore.service.DbConnectionConfigService;
import com.wq.httpclientcore.service.DbQueryConfigService;
import com.wq.httpclientcore.utils.QueryWrapperBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 *  服务实现类
 *
 * @author wangqi
 * @since 2025-03-12 14:31:39
 */
@Service
public class DbConnectionConfigServiceImpl extends ServiceImpl<DbConnectionConfigMapper, DbConnectionConfig> implements DbConnectionConfigService {

    @Autowired
    private ApplicationEventPublisher eventPublisher;

    @Autowired
    private DynamicDataSourceConfig dataSourceConfig;

    @Override
    public List<DbConnectionConfig> list(DbConnectionConfigREQ req) {
        req.setOrderByField("updated_at");
        LambdaQueryWrapper<DbConnectionConfig> dbConnectionConfigLambdaQueryWrapper = QueryWrapperBuilder.queryWrapper(DbConnectionConfig.class, req);
        return list(dbConnectionConfigLambdaQueryWrapper);
    }

    @Override
    public Page<DbConnectionConfig> pageList(DbConnectionConfigREQ req) {
        req.setOrderByField("updated_at");
        LambdaQueryWrapper<DbConnectionConfig> dbConnectionConfigLambdaQueryWrapper = QueryWrapperBuilder.queryWrapper(DbConnectionConfig.class, req);
        Page<DbConnectionConfig> page = Page.of(req.getPageNo(), req.getPageSize());
        return page(page, dbConnectionConfigLambdaQueryWrapper);
    }

    @Override
    public boolean saveDbConnectionConfig(DbConnectionConfig po) {
        int result = baseMapper.insertDbConnectionConfig(po);
        return result > 0;
    }

    @Override
    public boolean updateDbConnectionConfig(DbConnectionConfig po) {
        return baseMapper.updateDbConnectionConfigById(po);
    }

    @Override
    public Map<Long, DbConnectionConfig> id2PO(List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return new HashMap<>();
        }

        List<DbConnectionConfig> list = this.listByIds(ids);
        return list.stream().collect(Collectors.toMap(DbConnectionConfig::getId, e -> e));
    }

    @Transactional
    @Override
    public Boolean deletedById(List<Long> list) {
        try {
            boolean deletedConnectionFlag = removeByIds(list);
            if (deletedConnectionFlag) {
                eventPublisher.publishEvent(new DbConnectionConfigDeletedEvent(this, list));
                for (Long id : list) {
                    dataSourceConfig.evictDataSource(id);
                }
            }
            return deletedConnectionFlag;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

}
